<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
	String DRV = "org.mariadb.jdbc.Driver";
	String URL = "jdbc:mariadb://localhost:3306/jwj8508?user=jwj8508&password=123456";
	String sql = "select * from boardv4 where bdno = ?";
	String rsql = "update boardv4 set reades=reades+1 where bdno = ?";
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
%>
<%
	String bdno = null;
	String title = null;
	String uid = null;
	String wdate = null;
	String reades = null;
	String contents = null;
%>
<%
	bdno = request.getParameter("bdno");
	// 조회할 본문글 번호가 입력되지 않으면 자동으로 list.jsp로 이동시킴
	if (bdno == null) {
		response.sendRedirect("list.jsp");
	}

	try {
		Class.forName(DRV);
		conn = DriverManager.getConnection(URL);
		// 1. 조회수 증가
		pstmt = conn.prepareStatement(rsql);
		pstmt.setString(1, bdno);
		pstmt.executeUpdate();
		if (pstmt!=null) try {pstmt.close();} catch (Exception e){}
		// 2. 본문글 출력
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, bdno);
		rs = pstmt.executeQuery();
		
		if (rs.next()) {
			do {
				title = rs.getString("title");
				uid = rs.getString("uid");
				wdate = rs.getString("wdate");
				reades = rs.getString("reades");
				contents = rs.getString("contents");
				// Enter 키를 이용해서 입력한 줄바꿈은 html 상에서는 보이지 않음
				// 따라서, 이것을 <br />로 바꿔야 함
				//contents = contents.replace("\r\n", "<br />");	// 여기서 한번만 쓰면 되는데   updateok, writeok, update에 쓰는 이유는..
			} while (rs.next());
		} else {
			// 입력한 본문글 번호가 boardv4에 존재하지 않으면 자동으로 list.jsp로 이동시킴
			try {response.sendRedirect("list.jsp");} catch(Exception e){}
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (rs!=null)try{rs.close();}catch(Exception e){}
		if (pstmt!=null)try{pstmt.close();}catch(Exception e){}
		if (conn!=null)try{conn.close();}catch(Exception e){}
	}
%>
<!DOCTYPE html>
<html lang="ko">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>JSP 프로젝트 v1</title>
		<link type="text/css" href="../css/layout.css" rel="stylesheet" />
		<style type="text/css">			
			#view {
				width: 75%;
				margin: 0 auto;
			}
			label {
				display: inline-block;
				width: 10%;
				text-align: right;
				font-weight: bold;
				margin: 0 10px 10px 0;
			}
			.contents {	
				display: inline-block;
				width: 80%;
				height: 350px;
				overflow: auto;				
				padding: 0 10px 10px 10px;
			}
			.dragup {				
				vertical-align: top;
			}
		</style>
		
		
	</head>
	<body>
		<div>
			<h1>JSP 프로젝트 v1</h1> <!-- 제목 -->
			<ul id="nav">
				<li><a href="../index.html">Home</a></li>
				<li><a href="../member/join.html">회원가입</a></li>
				<li><a href="../member/login.html">로그인</a></li>
				<li><a href="list.html">게시판</a></li>
				<li><a href="../member/myinfo.html">회원정보</a></li>
			</ul> <!-- 상단메뉴 -->
			<hr />
		</div> <!-- header -->
		
		<div id="content">
			<h2>게시판 본문글</h2>
			<div id="view" name="view">
				<div><label>제목</label><span><%=title %></span></div>
				<div><label>작성자</label><span><%=uid %></span></div>
				<div><label>작성일</label><span><%=wdate %></span></div>
				<div><label>조회수</label><span><%=reades %></span></div>
				<div>
					<label class="dragup">본문</label>
					<span class="contents">
						<%=contents %>
					</span>
				</div>
				<div>
					<label> </label>
					<button type="button" id="updbtn">수정하기</button>
					<button type="button" id="delbtn">삭제하기</button>
				</div>
			</div>
		</div> <!-- content -->
		
		<div>
			<hr />
			<p id="copy">Copyright &copy; 2013 JINn. All right reserved.</p>
		</div> <!-- footer -->
		
		<script type="text/javascript">
			var deletebtn = document.getElementById("delbtn");
			deletebtn.onclick = function(){
				if(confirm('정말 삭제하시겠습니까?')) {					
					location.href="deleteok.jsp?bdno=" + <%=bdno %>;
				}
			};
			var updatebtn = document.getElementById("updbtn");
			updatebtn.onclick = function(){
				if(confirm('정말 수정하시겠습니까?')) {
					//alert("수정 되었습니다.");
					//location.href="update.html";
					location.href="update.jsp?bdno=" + <%=bdno %>;
				}
			};
		</script>
		
	</body>
	
	
</html>
